(function(e) {
    e.fn.autocomplete = function(t) {
        var n = -1;
        var r = [];
        t = e.extend({
            hints: [],
            placeholder: "请输入您要调研的关键词",
            showButton: true,
            buttonText: "Search",
            onSubmit: function(e) {},
            onBlur: function() {}
        }, t);
        this.each(function() {
            var i = e("<div></div>").addClass("autocomplete-container").css("height", t.height * 1.7);
            var s = e('<input type="text" autocomplete="off" name="query">').attr("placeholder", t.placeholder).addClass("autocomplete-input");
            var o = e("<div></div>").addClass("proposal-box");
            var u = e("<ul></ul>").addClass("proposal-list");
            var w = e("<i></i>").addClass("icon-delete js-search-delete");
            var d = e("<p><span class='js-del-history'>删除历史</span></p>").addClass("del-history");

            // var select = e('<select class="data-search-select type-select">'+
            //                 '<option value="1">窥探关键词</option>'+
            //                 '<option value="2">窥探流量词</option>'+
            //             '</select>');
            o.append(u);
            i.append(w);
            o.append(d);
            // if($("#search-form").attr("class") == "mult-choice"){
            //     i.append(select);
            //     s.css("width","64%");
            //     o.css('width',"68.9%");
            //     w.css("right","200px");
            //     s.attr("placeholder", "请输入子或父ASIN");
            // }
            if ($("#search-form").attr("class") == "ph-name") {
                s.attr("placeholder", "请输入子或父ASIN");
            } else if ($("#search-form").attr("class") == "ph-name2") {
                s.attr("placeholder", "请输入父ASIN或子ASIN");
            } else if ($("#search-form").attr("class") == "ph-name3") {
                s.attr("placeholder", "只能查询子ASIN");
            } else if ($("#search-form").attr("class") == "ph-name4") {
                s.attr("placeholder", "请输入(粘贴)reviews链接");
                i.append("<a class='link js-get-link'>?如何获取链接</a>");
            } else if ($("#search-form").attr("class") == "ph-name5") {
                s.attr("placeholder", "");
                i.append("<a class='link js-get-link'>?如何获取链接</a>");
            } else if ($("#search-form").attr("class") == "ph-name6") {
                s.attr("placeholder", "输入店铺链接（添加的店铺listing数量不得多于480个，如超过将导致监控失败）");
                i.append("<a class='link js-get-link'>?如何获取链接</a>");
            }

            s.keydown(function(i) {
                switch (i.which) {
                    case 38:
                        i.preventDefault();
                        e("ul.proposal-list li p").removeClass("selected");
                        if (n - 1 >= 0) {
                            n--;
                            e("ul.proposal-list li:eq(" + n + ")").addClass("selected")
                        } else {
                            n = -1
                        }
                        break;
                    case 40:
                        i.preventDefault();
                        if (n + 1 < r.length) {
                            e("ul.proposal-list li").removeClass("selected");
                            n++;
                            e("ul.proposal-list li:eq(" + n + ")").addClass("selected")
                        }
                        break;
                    case 13:
                        if (n > -1) {
                            var o = e("ul.proposal-list li:eq(" + n + ") p").html();
                            s.val(o)
                        }
                        n = -1;
                        u.empty();
                        t.onSubmit(s.val());
                        break;
                    case 27:
                        n = -1;
                        u.empty();
                        s.val("");
                        break
                }
            });
            var storage = window.localStorage;
            var len = storage.length;
            $(document).on("click", ".js-del-history", function() {
                $(".proposal-list").html("");
                $(".del-history").hide();
                // for(var i=0;i<len;i++){
                //     if(storage.key(i).indexOf("search")>=0){
                //       var val = storage.key(i);
                //       storage.removeItem(val);
                //     } 

                // }
                storage.clear();
                t.hints = [];
            })
            $(document).on("click", ".proposal .del", function() {
                var cont = $(this).siblings("p").html();
                var key = $(this).parent().attr("key");
                $(this).parent().remove();
                storage.setItem(key, "");
                for (var a in t.hints) {
                    if (t.hints[a] != undefined) {
                        var p = t.hints[a].split("$&");
                        if (cont == p[0]) {
                            t.hints.splice(a, 1);
                        }
                    }
                }
            });
            // 点击空白区域关闭
            $(document).click(function(e) {
                var _con = $('.autocomplete-container');
                if (!_con.is(e.target) && _con.has(e.target).length === 0) {
                    $(".proposal-list").html("");
                    $(".del-history").hide();
                }
            });

            s.bind("change paste keyup", function(i) {
                if (i.which != 13 && i.which != 27 && i.which != 38 && i.which != 40) {
                    r = [];
                    n = -1;
                    u.empty();
                    if (s.val() != "") {
                        var o = "^" + s.val() + ".*";
                        u.empty();
                        for (var a in t.hints) {
                            if (t.hints[a] != undefined) {
                                var p = t.hints[a].split("$&");
                                if (p[0].match(o) && p[0] != s.val()) {
                                    r.push(p[0]);
                                    var f = e("<li key='" + p[1] + "'></li>").html('<p>' + p[0] + '</p><b class="del">删除</b>').addClass("proposal");
                                    f.find("p").click(function() {
                                        s.val(e(this).html());
                                        u.empty();
                                        if ($(".proposal-list li").length > 0) {
                                            $(".del-history").show();
                                        } else {
                                            $(".del-history").hide();
                                        }
                                        t.onSubmit(s.val())
                                    });
                                    f.mouseenter(function() {
                                        e(this).addClass("selected")
                                    }).mouseleave(function() {
                                        e(this).removeClass("selected")
                                    });
                                    u.prepend(f);
                                }
                            }
                        }
                        var len = $(".proposal-list li").length;
                        for (var i = 8; i < len; i++) {
                            $(".proposal-list li").eq(i).addClass("hide");
                        }
                        $(".proposal-list li").each(function() {
                            if ($(this).hasClass("hide")) {
                                $(this).remove();
                            }
                        });

                    }
                }
                if ($(".proposal-list li").length > 0) {
                    $(".del-history").show();
                } else {
                    $(".del-history").hide();
                }
            });
            s.bind("change paste focus", function(i) {
                if (i.which != 13 && i.which != 27 && i.which != 38 && i.which != 40) {
                    r = [];
                    n = -1;
                    u.empty();
                    if (s.val() != "") {
                        var o = "^" + s.val() + ".*";
                        u.empty();
                        for (var a in t.hints) {
                            if (t.hints[a] != undefined) {
                                var p = t.hints[a].split("$&");
                                if (p[0].match(o) && p[0] != s.val()) {
                                    r.push(p[0]);
                                    var f = e("<li key='" + p[1] + "'></li>").html('<p>' + p[0] + '</p><b class="del">删除</b>').addClass("proposal");
                                    f.find("p").click(function() {
                                        s.val(e(this).html());
                                        u.empty();
                                        if ($(".proposal-list li").length > 0) {
                                            $(".del-history").show();
                                        } else {
                                            $(".del-history").hide();
                                        }
                                        t.onSubmit(s.val())
                                    });
                                    f.mouseenter(function() {
                                        e(this).addClass("selected")
                                    }).mouseleave(function() {
                                        e(this).removeClass("selected")
                                    });
                                    u.prepend(f);
                                }
                            }
                        }
                        var len = $(".proposal-list li").length;
                        for (var i = 8; i < len; i++) {
                            $(".proposal-list li").eq(i).addClass("hide");
                        }
                        $(".proposal-list li").each(function() {
                            if ($(this).hasClass("hide")) {
                                $(this).remove();
                            }
                        });

                    }
                }
                if ($(".proposal-list li").length > 0) {
                    $(".del-history").show();
                } else {
                    $(".del-history").hide();
                }
            });
            s.blur(function(e) {
                n = -1;
                t.onBlur();
            });
            i.append(s);
            i.append(o);
            if (t.showButton) {
                var a = e("<div></div>").addClass("autocomplete-button").html('<span style="font-size: 13px;color: #fff;font-family: "Microsoft YaHei";opacity: 10;">查&nbsp;询</span>').click(function() {
                    u.empty();
                    t.onSubmit(s.val());
                    $(".del-history").hide();
                });
                i.append(a);
                // i.append(b);
                if ($("#search-form").attr("class") == "mult-choice") {
                    a.css("margin-left", "100px");
                }
            }
            e(this).append(i);
            if (t.showButton) {
                i.css("width", t.width + a.width() + 50)
            }
        });
        return this
    }
})(jQuery)